package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;
import com.xiaoxing.salesclient.di.component.DaggerGeRenZhongXinComponent;
import com.xiaoxing.salesclient.di.module.GeRenZhongXinModule;
import com.xiaoxing.salesclient.mvp.contract.GeRenZhongXinContract;
import com.xiaoxing.salesclient.mvp.model.entity.OrderPay;
import com.xiaoxing.salesclient.mvp.model.entity.UserCenter;
import com.xiaoxing.salesclient.mvp.presenter.GeRenZhongXinPresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.GeRenZhongXinDingDanJiLuAdapter;
import com.xiaoxing.salesclient.mvp.ui.adapter.GeRenZhongXinHuiZhenShiJianAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_GE_REN_ZHONG_XIN_ACTIVITY)
public class GeRenZhongXinActivity extends BaseActivity<GeRenZhongXinPresenter> implements GeRenZhongXinContract.View, GeRenZhongXinDingDanJiLuAdapter.Pay {


    @BindView(R2.id.cimg_head)
    ImageView cimgHead;
    @BindView(R2.id.tv_xing_ming)
    TextView tvXingMing;
    @BindView(R2.id.tv_zhang_hao)
    TextView tvZhangHao;
    @BindView(R2.id.tv_sheng_yu_jin_e)
    TextView tvShengYuJinE;
    @BindView(R2.id.tv_lei_ji_ji_fen)
    TextView tvLeiJiJiFen;
    @BindView(R2.id.tv_shang_pin_dui_huan)
    TextView tvShangPinDuiHuan;
    @BindView(R2.id.tv_hui_zhen_shi_jian)
    TextView tv_hui_zhen_shi_jian;
    @BindView(R2.id.img_level)
    ImageView imgLevel;
    @BindView(R2.id.img_vip)
    ImageView imgVip;
    @BindView(R2.id.img_er_wei_ma)
    ImageView imgErWeiMa;
    @BindView(R2.id.ll_no_data_ding_dan_ji_lu)
    LinearLayout tvDingDanTip;
    @BindView(R2.id.ll_no_data_hui_zhen_shi_jian)
    LinearLayout tvHuiZhenTip;

    private GeRenZhongXinDingDanJiLuAdapter mGeRenZhongXinDingDanJiLuAdapter;
    private GeRenZhongXinHuiZhenShiJianAdapter mGeRenZhongXinHuiZhenShiJianAdapter;
    private RecyclerView mGeRenZhongXinDingDanJiLuRecyclerView;
    private RecyclerView mGeRenZhongXinHuiZhenShiJianRecyclerView;
    private List<UserCenter.DataBean.OrderBean> mOrderBeanList = new ArrayList<>();
    private List<UserCenter.DataBean.HistoryBean> mHistoryBeanList = new ArrayList<>();
    private String mQRImgPath;

    @Autowired
    String uid;

    @Override
    protected void onResume() {
        super.onResume();
        tvXingMing.setText("姓名：" + mSharedPreferencesHelper.getString(BaseConstants.NICKNAME));
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerGeRenZhongXinComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .geRenZhongXinModule(new GeRenZhongXinModule(this))
                .build()
                .inject(this);
        ARouter.getInstance().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_ge_ren_zhong_xin; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.sales_client_ge_ren_zhong_xin));

//        initLevel();
        initGeRenZhongXinDingDanJiLuRecyclerView();
        initGeRenZhongXinHuiZhenShiJianRecyclerView();

        getUserCenter();

    }

    private void initLevel(String level) {
//        String level = mSharedPreferencesHelper.getString(BaseConstants.LEVEL);
        if (level.equals("1")) {
            Glide.with(this).load(R.drawable.icon_ge_ren_zhong_xin_level_v1).into(imgLevel);
        } else if (level.equals("2")) {
            Glide.with(this).load(R.drawable.icon_ge_ren_zhong_xin_level_v2).into(imgLevel);
        } else if (level.equals("3")) {
            Glide.with(this).load(R.drawable.icon_ge_ren_zhong_xin_level_v3).into(imgLevel);
        } else if (level.equals("4")) {
            Glide.with(this).load(R.drawable.icon_ge_ren_zhong_xin_level_v4).into(imgLevel);
        } else if (level.equals("5")) {
            Glide.with(this).load(R.drawable.icon_ge_ren_zhong_xin_level_v5).into(imgLevel);
        }
    }

    private void getUserCenter() {
        if (!TextUtils.isEmpty(uid)) {
            mPresenter.userInfo(mSharedPreferencesHelper.getString(BaseConstants.TOKEN), uid);
        } else {
            mPresenter.userCenter(mSharedPreferencesHelper.getString(BaseConstants.TOKEN));
        }
    }

    @Override
    public void showLoading() {
        LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtil.dismissDialog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R2.id.tv_shang_pin_dui_huan, R2.id.img_er_wei_ma, R2.id.cimg_head, R2.id.tv_xing_ming, R2.id.tv_zhang_hao, R2.id.tv_sheng_yu_jin_e, R2.id.tv_lei_ji_ji_fen})
    void OnClick(View view) {
        int id = view.getId();

        if (id == R.id.tv_shang_pin_dui_huan) {

            Utils.navigation(GeRenZhongXinActivity.this, RouterHub.SALES_CLIENT_JI_FEN_SHANG_CHENG_ACTIVITY);

        } else if (id == R.id.img_er_wei_ma) {

            if (!TextUtils.isEmpty(mQRImgPath)) {
                ARouter.getInstance().build(RouterHub.SALES_CLIENT_IMAGE_LOOK_ACTIVITY).withString("imgPath", mQRImgPath).navigation();
            }

        } else if (id == R.id.cimg_head || id == R.id.tv_xing_ming || id == R.id.tv_zhang_hao || id == R.id.tv_sheng_yu_jin_e || id == R.id.tv_lei_ji_ji_fen) {


            if (mSharedPreferencesHelper.getString(BaseConstants.IS_STAFF).equals("1")) {//0是用户，1是员工
            } else {
                if (!TextUtils.isEmpty(uid)) {
                    mSharedPreferencesHelper.putString("gu_ke_uid", uid);
                }
                Utils.navigation(GeRenZhongXinActivity.this, RouterHub.SALES_CLIENT_SHE_ZHI_ACTIVITY);
            }


        }


    }

    @Override
    public void userCenterSuccessed(UserCenter userCenter) {

        if (userCenter != null && userCenter.getData() != null) {

            UserCenter.DataBean dataBean = userCenter.getData();

            String nickname = TextUtils.isEmpty(dataBean.getNick_name()) ? "" : dataBean.getNick_name();
            tvXingMing.setText("姓名：" + nickname);
            tvZhangHao.setText("帳號：" + dataBean.getUser_name());
            tvShengYuJinE.setText("剩餘金額：" + dataBean.getMember_money());
            tvLeiJiJiFen.setText("累計積分：" + dataBean.getPoints());


            initLevel(dataBean.getLevel());
            RequestOptions mRequestOptions = RequestOptions.circleCropTransform()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                    .skipMemoryCache(true);//不做内存缓存
            Glide.with(this).load(dataBean.getCover()).apply(mRequestOptions).into(cimgHead);

            mSharedPreferencesHelper.putString(BaseConstants.NICKNAME, dataBean.getNick_name());
            mSharedPreferencesHelper.putString(BaseConstants.COVER, dataBean.getCover());
            Glide.with(this).load(dataBean.getQrcode()).into(imgErWeiMa);
            mQRImgPath = dataBean.getQrcode();

            if (dataBean.getOrder() != null && dataBean.getOrder().size() > 0) {
                tvDingDanTip.setVisibility(View.GONE);
                mGeRenZhongXinDingDanJiLuRecyclerView.setVisibility(View.VISIBLE);
                mOrderBeanList.clear();
                mOrderBeanList.addAll(dataBean.getOrder());
                mGeRenZhongXinDingDanJiLuAdapter.notifyDataSetChanged();
            } else {
                tvDingDanTip.setVisibility(View.VISIBLE);
                mGeRenZhongXinDingDanJiLuRecyclerView.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(uid)) {
                tv_hui_zhen_shi_jian.setVisibility(View.GONE);
                tvHuiZhenTip.setVisibility(View.GONE);
                mGeRenZhongXinHuiZhenShiJianRecyclerView.setVisibility(View.GONE);
            } else {

                if (dataBean.getHistory() != null && dataBean.getHistory().size() > 0) {

                    tvHuiZhenTip.setVisibility(View.GONE);
                    mGeRenZhongXinHuiZhenShiJianRecyclerView.setVisibility(View.VISIBLE);
                    mHistoryBeanList.clear();
                    mHistoryBeanList.addAll(dataBean.getHistory());
                    mGeRenZhongXinHuiZhenShiJianAdapter.notifyDataSetChanged();
                } else {
                    tvHuiZhenTip.setVisibility(View.VISIBLE);
                    mGeRenZhongXinHuiZhenShiJianRecyclerView.setVisibility(View.GONE);
                }
            }

        }

    }


    @Override
    public void orderPaySuccess(OrderPay orderPay) {
//        if (orderPay != null && orderPay.getData() != null) {
//            WebviewUtil.loadData(this, web, orderPay.getData());
//        }
    }


    private void initGeRenZhongXinDingDanJiLuRecyclerView() {
        mGeRenZhongXinDingDanJiLuRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_ding_dan);
        mGeRenZhongXinDingDanJiLuAdapter = new GeRenZhongXinDingDanJiLuAdapter(this, mOrderBeanList, this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mGeRenZhongXinDingDanJiLuRecyclerView.setLayoutManager(linearLayoutManager);
        mGeRenZhongXinDingDanJiLuRecyclerView.setAdapter(mGeRenZhongXinDingDanJiLuAdapter);
    }

    private void initGeRenZhongXinHuiZhenShiJianRecyclerView() {
        mGeRenZhongXinHuiZhenShiJianRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_hui_zhen);
        mGeRenZhongXinHuiZhenShiJianAdapter = new GeRenZhongXinHuiZhenShiJianAdapter(this, mHistoryBeanList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mGeRenZhongXinHuiZhenShiJianRecyclerView.setLayoutManager(linearLayoutManager);
        mGeRenZhongXinHuiZhenShiJianRecyclerView.setAdapter(mGeRenZhongXinHuiZhenShiJianAdapter);
    }


    @Override
    public void payOrder(String order_no) {
//        mPresenter.orderPay(mSharedPreferencesHelper.getString(BaseConstants.TOKEN), order_no);
        ARouter.getInstance().build(RouterHub.SALES_CLIENT_PAY_ACTIVITY).withString("order_no", order_no).navigation();
    }
}
